package com.gzmu.face.mapper;

import com.gzmu.face.entity.Permission;
import com.gzmu.face.entity.Role;
import com.gzmu.face.entity.User;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author xiaobo Yan
 * @since 2020-04-20
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Insert("INSERT INTO t_user(parent,name,passwd,nickname,role,picurl,createtime,updatetime,remark) VALUES(#{parent}," +
            "#{name},#{passwd},#{nickname},#{role},#{picurl},now(),now(),#{remark}) ON DUPLICATE KEY UPDATE parent=#{parent}," +
            "name=#{name},passwd=#{passwd},nickname=#{nickname},role=#{role},picurl=#{picurl},updatetime=now(),remark=#{remark}")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer insertUser(User user);

    @Select("select * from t_user where name=#{name}")
    User getUserByUserName(@Param( "name" ) String name);

    @Select( "SELECT r.* FROM t_user u,t_user_role ur,t_role r WHERE u.id=ur.roleid AND ur.roleid=r.id AND u.name=#{name}" )
    List<Role> getRolesByUserName(@Param( "name" ) String name);

    @Select( "SELECT r.name FROM t_user u,t_user_role ur,t_role r WHERE u.id=ur.roleid AND ur.roleid=r.id AND u.name=#{name}" )
    List<String> getRolesNameByUserName(@Param( "name" ) String name);

    @Select( "SELECT p.* FROM t_user u,t_user_role ur,t_role_permission rp,t_permission p WHERE u.id=ur.userid and " +
            "ur.roleid=rp.roleid and rp.permissionid=p.id and u.name=#{name}" )
    List<Permission> getPermissionsByUserName(@Param( "name" ) String name);

    @Select( "SELECT p.name FROM t_user u,t_user_role ur,t_role_permission rp,t_permission p WHERE u.id=ur.userid and " +
            "ur.roleid=rp.roleid and rp.permissionid=p.id and u.name=#{name}" )
    List<String> getPermissionsNameByUserName(@Param( "name" ) String name);
}
